﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;
using Fly.Common.Linq;

namespace RN.ERP.Web.Enterprise
{
    public partial class EnterpriseSafeCertList : BasePage
    {
        public override bool IsCheckPermission
        {
            get
            {
                return true;
            }
        }
        private EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
        private BasicService.BasicServiceClient basicServiceClient = new BasicService.BasicServiceClient();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFlowCode();
            }
            
        }

        /// <summary>
        /// 客户端grid刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "Rebind")
            {
                ListCorps.MasterTableView.SortExpressions.Clear();
                ListCorps.MasterTableView.GroupByExpressions.Clear();
                ListCorps.Rebind();
            }

        }
        protected void ListCorps_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            //string ClassId = Request.QueryString["classId"];
            int count = 0;
            var queryCondition = hdnCondition.Value;
            if (string.IsNullOrEmpty(queryCondition))
            {
                queryCondition = GetCondition();
            }

            var securityStr = enterpriseServiceClient.GetCorpSecurityList(out count, queryCondition, ListCorps.PageSize, ListCorps.CurrentPageIndex + 1, UserInfo.CurrentArea);
            var securityList = JsonConvert.DeserializeObject<IList<Kh_CorpTable>>(securityStr);

            ListCorps.AllowCustomPaging = true;
            ListCorps.VirtualItemCount = count;
            ListCorps.DataSource = securityList;
        }

        private string GetCondition()
        {
            List<QueryCompanyInfoCondition> queryCompanyInfoList = new List<QueryCompanyInfoCondition>();

            QueryCompanyInfoCondition queryCompanyInfoCondition = new QueryCompanyInfoCondition();

            string corpName = txtCorpName.Text.Trim();
            List<string> listFlow = GetFlowCodes();
            if (listFlow.Count > 0)
            {
                foreach (var flow in listFlow)
                {
                    QueryCompanyInfoCondition con = new QueryCompanyInfoCondition();
                    
                    if (!string.IsNullOrEmpty(corpName))
                    {
                        con.CorpName = new ParameterValue<string>() { O = OptEnum.Like, V = corpName };
                    }
                    
                    con.FlowCode = new ParameterValue<string>() { O = OptEnum.Equal, V = flow };
                    queryCompanyInfoList.Add(con);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(corpName))
                {
                    queryCompanyInfoCondition.CorpName = new ParameterValue<string>() { O = OptEnum.Like, V = corpName };
                }
                queryCompanyInfoCondition.FlowCode = new ParameterValue<string>() { O = OptEnum.Like, V = "019" };
                
                queryCompanyInfoList.Add(queryCompanyInfoCondition);
            }

            return JsonConvert.SerializeObject(queryCompanyInfoList);
        }


        private List<string> GetFlowCodes()
        {
            string flowCode = ddlStatus.SelectedValue;
            List<string> listFlow = new List<string>();
            foreach (var f in flowCode.Split('|'))
            {
                if (!string.IsNullOrEmpty(f))
                {
                    listFlow.Add(f);
                }
            }
            return listFlow;
        }

        private void BindFlowCode()
        {
            string classid = Request.QueryString["classid"];
            string msg = string.Empty;
            var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(basicServiceClient.GetAllFlowStatus(out msg, UserInfo.CurrentArea, UserInfo.RoleId, classid));
            ddlStatus.Items.Clear();
            foreach (var key in dic.Keys)
            {
                ddlStatus.Items.Add(new ListItem(key, dic[key]));
            }
            ddlStatus.Items.Insert(0, new ListItem("全部", ""));
            //if (dic.ContainsKey("处理中"))
            //{
            //    ddlStatus.Items.FindByText("处理中").Selected = true;
            //}
        }

        
    }
}